import 'dart:async';

import '../bindings/app_routes.dart';
import 'package:get/get.dart';
import '../api/api_client.dart';

import '../utils/custom_widget.dart';
import '../utils/sava_cookie.dart';

// 登陆
class LoginController extends GetxController {
  Future<void> login(String email, String password) async {
    try {
      final response = await ApiClient.dio.post(
        ApiEndpoints.login,
        data: {'email': email, 'password': password},
      );
      final data = response.data;

      if (response.statusCode == 200 && data?['success'] == 1) {
        saveCookie(response);
        Get.offAllNamed(AppRoutes.tabs);
      } else {
        showToast('登录失败', data?['errors'].values.first, isError: true);
      }
    } catch (e) {
      showToast('错误', '发生异常: $e', isError: true);
    }
  }
}

//注册
class RegisterController extends GetxController {
  Future<void> register(
    String email,
    String password,
    String repassword,
    String user_name,
  ) async {
    try {
      final response = await ApiClient.dio.post(
        ApiEndpoints.register,
        data: {
          'email': email,
          'password': password,
          'repassword': repassword,
          'user_name': user_name,
        },
      );
      final data = response.data;
      if (response.statusCode == 200 && data?['success'] == 1) {
        showDialog('注册成功', '请返回登录页进行登录', AppRoutes.login);
      } else {
        showToast('注册失败', data?['errors'].values.first, isError: true);
      }
    } catch (e) {
      showToast('错误', '发生异常: $e', isError: true);
    }
  }
}

//退出登录
class LogoutController extends GetxController {
  Future<void> logout() async {
    try {
      await ApiClient.dio.get(ApiEndpoints.logout);
    } catch (e) {}
  }
}

//找回密码
class ForgotPasswordController extends GetxController {
  String user_email = '';
  var countdown = 0.obs;
  var is_button_enable = true.obs;

  Future<void> forgotPasswordStepOne(String email, String captcha) async {
    user_email = email;
    try {
      final response = await ApiClient.dio.get(
        ApiEndpoints.forgot_password,
        queryParameters: {'email': email, 'captcha': captcha},
      );
      final data = response.data;
      if (response.statusCode == 200 && data?['success'] == 1) {
        Get.offNamed(AppRoutes.forgotPasswordStepTwo);
      } else {
        showToast('提交失败', data?['errors'].values.first, isError: true);
      }
    } catch (e) {
      showToast('错误', '发生异常: $e', isError: true);
    }
  }

  void startCountdown() {
    countdown.value = 10;
    is_button_enable.value = false;
    Timer.periodic(Duration(seconds: 1), (timer) {
      if (countdown.value > 0) {
        countdown.value--;
      } else {
        timer.cancel();
        is_button_enable.value = true;
      }
    });
  }

  Future<void> forgotPasswordStepTwo(String password, String repassword) async {
    try {
      final response = await ApiClient.dio.post(
        ApiEndpoints.forgot_password,
        data: {
          'email': user_email,
          'password': password,
          'repassword': repassword,
        },
      );
      final data = response.data;
      if (response.statusCode == 200 && data?['success'] == 1) {
        showDialog('修改成功', '请返回登录页进行登录', AppRoutes.login);
      } else {
        showToast('修改失败', data?['errors'].values.first, isError: true);
      }
    } catch (e) {
      showToast('错误', '发生异常: $e', isError: true);
    }
  }
}
